"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.HMModule = void 0;
const fs = require("fs");
const path = require("path");
const Uitls_1 = require("../src/Uitls");
const ImageEntiry_1 = require("../src/ImageEntiry");
const config_1 = require("../src/config");
class HMModule {
    name;
    srcPath;
    constructor(name, path) {
        this.name = name;
        this.srcPath = path;
    }
    static allModules() {
        let config_path = path.resolve(config_1.project_dir, 'build-profile.json5');
        let config = JSON.parse((0, Uitls_1.removeJsonComments)(fs.readFileSync(config_path, 'utf-8')));
        return config.modules.map(e => new HMModule(e.name, e.srcPath));
    }
    get hasResource() {
        return fs.existsSync(path.resolve(config_1.project_dir, this.srcPath, config_1.img_resource_path));
    }
    get allImageResource() {
        let dir_path = path.resolve(config_1.project_dir, this.srcPath, config_1.img_resource_path);
        if (!fs.existsSync(dir_path)) {
            return [];
        }
        let imgs = fs.readdirSync(dir_path);
        return imgs.map(e => new ImageEntiry_1.ImageEntity(e, path.resolve(dir_path, e)));
    }
    traverseEtsFile(handler) {
        let rootPath = path.resolve(config_1.project_dir, this.srcPath, 'src/main/ets');
        let stack = [rootPath];
        while (stack.length > 0) {
            let cur = stack.pop();
            let files = fs.readdirSync(cur);
            for (let file of files) {
                let file_path = path.resolve(cur, file);
                if (fs.statSync(file_path).isDirectory()) {
                    stack.push(file_path);
                }
                else if (file.endsWith('.ets')) {
                    handler(file_path);
                }
            }
        }
    }
    handleRepeatImage() {
        let dir_path = path.resolve(config_1.project_dir, this.srcPath, config_1.img_resource_path);
        let map = new Map();
        let imgs = fs.readdirSync(dir_path);
        for (let img of imgs) {
            let img_path = path.resolve(dir_path, img);
            let md5 = (0, Uitls_1.getImageMD5Sync)(img_path);
            let entry = new ImageEntiry_1.ImageEntity(img, img_path);
            if (map.has(md5)) {
                map.get(md5).push(entry);
            }
            else {
                map.set(md5, [entry]);
            }
        }
        let res = [...map].filter(e => e[1].length > 1).map(e => e[1].map(ee => ee.name));
        return res;
    }
    handleUnusedImage() {
    }
}
exports.HMModule = HMModule;
